﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Data.SqlClient;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using DAO_ADMIN;
using PTTK19.DB;

namespace PTTK19.GUI
{
    public partial class ThucDonFrm : Form
    {
        private int _ID;
        private int _curID;
        private bool _confirm = false;
        public ThucDonFrm()
        {
            _ID = 0;
            InitializeComponent();
        }
        public ThucDonFrm(int id)
        {
            _ID = id;
            InitializeComponent();
        }


        private void btmRefreh_Click(object sender, EventArgs e)
        {
            LoaddgvDanhSachThucDon();
            dgvMonAn.DataSource = null;
        }

        private void btnThemThucDon_Click(object sender, EventArgs e)
        {
            if (_confirm == false)
            {
                txtTenThucDon.ReadOnly = false;
                txtTinhTrang.ReadOnly = false;
                txtMaThucDon.Text = "--";
                txtTenThucDon.Text = "";
                DateTime dt = DateTime.Now;
                txtNgayLap.Text = dt.Date.ToShortDateString();
                txtTinhTrang.Text = "";
                btnCapNhatThucDon.Enabled = false;
                btnXoaThucDon.Enabled = false;
                btmRefreh.Enabled = false;
                btnThemThucDon.Text = "Xác nhận";
                _confirm = true;
            }
            else
            {
                //Add
                string _TenMonAn = txtTenThucDon.Text;
                int _TinhTrang = int.Parse(txtTinhTrang.Text);

                bool kq = DBThucDon.AddThucDon(_TenMonAn, _TinhTrang);
                LoaddgvDanhSachThucDon();
                _curID = (int)dgvThucDon.Rows[dgvThucDon.Rows.Count - 1].Cells[0].Value;
                ChiTietMonAnFrm frm = new ChiTietMonAnFrm(_curID);
                frm.ShowDialog();
                if (kq)
                {
                    MessageBox.Show("Thêm thành công!");
                }
                else
                {
                    MessageBox.Show("Thêm thất bại!");
                }

                //Setting
                txtTenThucDon.ReadOnly = true;
                txtTinhTrang.ReadOnly = true;
                txtMaThucDon.Text = "--";
                txtTenThucDon.Text = "";
                txtNgayLap.Text = "";
                txtTinhTrang.Text = "";
                btnCapNhatThucDon.Enabled = true;
                btnXoaThucDon.Enabled = true;
                btmRefreh.Enabled = true;
                btnThemThucDon.Text = "Thêm thực đơn";
                _confirm = false;
            }
        }

        private void btnCapNhatThucDon_Click(object sender, EventArgs e)
        {
            if (_confirm == false)
            {
                txtTenThucDon.ReadOnly = false;
                txtTinhTrang.ReadOnly = false;
                btnCapNhatThucDon.Enabled = false;
                btnXoaThucDon.Enabled = false;
                btmRefreh.Enabled = false;
                btnThemThucDon.Text = "Xác nhận";
                _confirm = true;
            }
            else
            {
                //Add
                int _MaThucDon = int.Parse(txtMaThucDon.Text);
                string _TenThucDon = txtTenThucDon.Text;
                int _TinhTrang = int.Parse(txtTinhTrang.Text);

                bool kq = DBThucDon.UpdateThucDon(_MaThucDon, _TenThucDon ,_TinhTrang);
                LoaddgvDanhSachThucDon();
                _curID = (int)dgvThucDon.Rows[dgvThucDon.Rows.Count - 1].Cells[0].Value;
                ChiTietMonAnFrm frm = new ChiTietMonAnFrm(_curID);
                frm.ShowDialog();
                if (kq)
                {
                    MessageBox.Show("Câp nhật thành công!");
                }
                else
                {
                    MessageBox.Show("Cập nhật thất bại!");
                }

                //Setting
                txtTenThucDon.ReadOnly = true;
                txtTinhTrang.ReadOnly = true;
                txtMaThucDon.Text = "--";
                txtTenThucDon.Text = "";
                txtNgayLap.Text = "";
                txtTinhTrang.Text = "";
                btnCapNhatThucDon.Enabled = true;
                btnXoaThucDon.Enabled = true;
                btmRefreh.Enabled = true;
                btnThemThucDon.Text = "Cập nhật thực đơn";
                _confirm = false;
            }
        }

        private void btnXoaThucDon_Click(object sender, EventArgs e)
        {
            if (_confirm == false)
            {
                btnCapNhatThucDon.Enabled = false;
                btnXoaThucDon.Enabled = false;
                btmRefreh.Enabled = false;
                btnThemThucDon.Text = "Xác nhận";
                _confirm = true;
            }
            else
            {
                //Add
                int _MaThucDon = int.Parse(txtMaThucDon.Text);

                bool kq = DBThucDon.DelThucDon(_MaThucDon);
                LoaddgvDanhSachThucDon();
                _curID = (int)dgvThucDon.Rows[dgvThucDon.Rows.Count - 1].Cells[0].Value;
                if (kq)
                {
                    MessageBox.Show("Xóa thành công!");
                }
                else
                {
                    MessageBox.Show("Xóa thất bại!");
                }

                //Setting
                txtMaThucDon.Text = "--";
                txtTenThucDon.Text = "";
                txtNgayLap.Text = "";
                txtTinhTrang.Text = "";
                btnCapNhatThucDon.Enabled = true;
                btnXoaThucDon.Enabled = true;
                btmRefreh.Enabled = true;
                btnThemThucDon.Text = "Xóa thực đơn";
                _confirm = false;
            }
        }

        private void btnThoat_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void btnThayDoi_Click(object sender, EventArgs e)
        {
            ChiTietMonAnFrm frm = new ChiTietMonAnFrm(_curID);
            frm.ShowDialog();
            LoadDgvMonAn();
        }

        private void LoadDgvMonAn()
        {
            DataTable dt = DBMonAn.ChiTietMonAn(_curID);
            dgvMonAn.DataSource = dt;
        }

        private void ThucDonFrm_Load(object sender, EventArgs e)
        {
            btnThayDoi.Enabled = true;
            LoaddgvDanhSachThucDon();
            if (_ID == 0)
            {
                
            }
            else
            {

            }
        }

        private void LoaddgvDanhSachThucDon()
        {
            DataTable dt = DBThucDon.GetList();
            dgvThucDon.DataSource = dt;
        }

        private void dgvThucDon_CellClick(object sender, DataGridViewCellEventArgs e)
        {
            try
            {
                int index = dgvThucDon.CurrentRow.Index;
                if (dgvThucDon.CurrentRow != null)
                {
                    //lay ma mon an
                    _curID = (int)dgvThucDon.Rows[index].Cells[0].Value;
                    txtMaThucDon.Text = _curID.ToString();
                    txtTenThucDon.Text = (string)dgvThucDon.Rows[index].Cells[1].Value;
                    DateTime dt = (DateTime)dgvThucDon.Rows[index].Cells[2].Value;
                    txtNgayLap.Text = dt.Date.ToShortDateString();
                    txtTinhTrang.Text = ((int)dgvThucDon.Rows[index].Cells[3].Value).ToString();
                    LoadDgvMonAn();
                }
            }
            catch (Exception ex)
            { }
        }

        private void txtTinhTrang_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (!Char.IsDigit(e.KeyChar) && !Char.IsControl(e.KeyChar))
            {
                e.Handled = true;
                errorProvider1.SetError(txtTinhTrang, "Không được nhập chữ");
            }
        }
    }
}
